/**
 * Created by charsen on 16/5/27.
 */
directives.directive('selectable', ["$rootScope", "tools", "$timeout", '$routeParams', "tmp", function ($rootScope, tools, $timeout, $routeParams, tmp) {
    return {
        restrict: 'A',
        scope: false,
        link: function (scope, element, attr) {
            var identifier = tools.identifier();
            var $element = $(element);
            var events = {};

            // scope.selected = false;

            if (tmp.data.previewing) {
                scope.previewing = true;
            } else {
                scope.previewing = false;
            }

            if (attr.type != 'text') {
                if (((!attr.moduleid && !attr.conditionid))) {
                    $timeout(function () {
                        scope.selected = scope.config.selected = true;
                        $element.css('zIndex', 10);
                        $rootScope.$broadcast('UNSELECT_EV', identifier);
                        scope.recover_fields && scope.recover_fields();
                    }, 50);
                }
            }

            if (attr.type == 'text' && !attr.content) {
                $timeout(function () {
                    scope.selected = scope.config.selected = true;
                    $element.css('zIndex', 10);
                    $rootScope.$broadcast('UNSELECT_EV', identifier);
                    scope.recover_fields && scope.recover_fields();
                }, 50);
            }

            scope.select = function () {
                if (scope.moving || scope.resizing || scope.previewing || scope.config.linking) {
                    return;
                }

                scope.config.selected = scope.selected = !scope.selected;
                $element.css('zIndex', 10);

                if (scope.selected) {
                    $rootScope.$broadcast('UNSELECT_EV', identifier);
                    scope.recover_fields && scope.recover_fields(true);
                } else {
                    scope.recover_fields && scope.recover_fields(false);
                }

                $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
            }

            events.UNSELECT_EV_OFF = $rootScope.$on('UNSELECT_EV', function (e, identifier_) {
                if (identifier != identifier_) {
                    if (scope.selected) {
                        scope.recover_fields && scope.recover_fields(false);
                        $timeout(function () {
                            scope.config.selected = scope.selected = false;
                        }, 0);
                        $element.css('zIndex', 0);
                    }
                }
            });

            events.PREVIEW_EV_OFF = $rootScope.$on('PREVIEW_EV', function () {
                $timeout(function () {
                    scope.previewing = true;
                    $element.removeClass('border');
                }, 0);
            });

            events.PREVIEW_OFF_EV_OFF = $rootScope.$on('PREVIEW_OFF_EV', function () {
                $timeout(function () {
                    scope.previewing = false;
                    $element.addClass('border');
                }, 0);
            });

            scope.$on('$destroy', function () {
                for (var i in events) {
                    if (i.indexOf('_OFF') != -1) {
                        events[i]();
                    }
                }
            });
        }
    }
}]);